package com.haiqiu.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.haiqiu.common.controller.BaseController;
import com.haiqiu.common.page.PageRequest;
import com.haiqiu.common.result.Constants;
import com.haiqiu.common.result.ResultData;
import com.haiqiu.system.entity.SysDept;
import com.haiqiu.system.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author haiqiu
 */
@Api(tags = "部门管理",description = "部门的增删改查")
@RestController
@RequestMapping("/dept")
public class DeptController extends BaseController {

    @Autowired
    private SysDeptService sysDeptService;

    @ApiOperation(value = "所有部门")
    @RequestMapping(method = RequestMethod.GET,value = "/all")
    public ResultData all (@ApiParam("部门名称") @RequestParam(required = false) String deptName,
                           @ApiParam("激活状态：true激活，false禁用") @RequestParam(required = false) Boolean status){
        return decide(sysDeptService.all(deptName,status));
    }

    @ApiOperation(value = "部门树形")
    @RequestMapping(method = RequestMethod.POST,value = "/tree")
    public ResultData tree (){
        return decide(sysDeptService.tree());
    }

    @ApiOperation(value = "部门新增")
    @RequestMapping(method = RequestMethod.POST,value = "")
    public ResultData save (@RequestBody @Validated SysDept sysDept){
        sysDeptService.save(sysDept);
        return decide(Constants.SUCCESS_ADD);
    }

    @ApiOperation(value = "部门查询")
    @RequestMapping(method = RequestMethod.GET,value = "")
    public ResultData get (@RequestParam Long deptId){
        return decide(sysDeptService.get(deptId));
    }


    @ApiOperation(value = "部门批量删除")
    @RequestMapping(method = RequestMethod.DELETE,value = "")
    public ResultData delete (@RequestBody Set<Long> ids){
        return decide(Constants.DELETE, sysDeptService.delete(ids));
    }

    @ApiOperation(value = "部门修改")
    @RequestMapping(method = RequestMethod.PUT,value = "")
    public ResultData update (@RequestBody @Validated SysDept sysDept){
        sysDeptService.update(sysDept);
        return decide(Constants.SUCCESS_UPDATE);
    }

    @ApiOperation(value = "部门批量激活")
    @RequestMapping(method = RequestMethod.PUT,value = "/active")
    public ResultData active(@ApiParam("部门ID，数组[]") @RequestBody List<SysDept> sysDepts){
        return decide(Constants.ACTIVE, sysDeptService.active(sysDepts));
    }

    @ApiOperation(value = "部门模糊分页")
    @RequestMapping(method = RequestMethod.POST,value = "/list")
    public ResultData list (@RequestBody PageRequest<SysDept> request){
        return decide(sysDeptService.list(request));
    }

    @ApiOperation(value = "部门树形模糊分页")
    @RequestMapping(method = RequestMethod.POST,value = "/tree/list")
    public ResultData treeList (@RequestBody PageRequest<SysDept> request){
        return decide(sysDeptService.treeList(request));
    }
}
